package com.Pan.util;

import org.springframework.stereotype.Component;

/**
 * @Description: 通用响应结果
 * @Author: zhangjie
 * @Date: 16/8/18 AM10:18
 */
@Component
public class ResponseData<T>  {

    /**
     * 响应状态
     */
    private RetStatus retStatus;

    /**
     * 响应结果
     */
    private T retData;

    public ResponseData() {
    }
    /**
     * 无数据返回
     * @param retStatus 状态码
     */
    public ResponseData(RetStatus retStatus) {
        this.retStatus = retStatus;
    }

    /**
     * 返回单个对象
     * @param retStatus 状态码
     * @param data  数据
     */
    public ResponseData(RetStatus retStatus, T data) {
        this.retStatus = retStatus;
        this.retData = data;
    }

    public T getRetData() {
        return retData;
    }

    public void setRetData(T retData) {
        this.retData = retData;
    }
    public RetStatus getRetStatus() {
        return retStatus;
    }
    public void setRetStatus(RetStatus retStatus) {
        this.retStatus = retStatus;
    }

    /**
     * 设置成功状态
     *
     * @date  16/8/18 AM10:50
     * @return ResponseData<T>
     */
    public static ResponseData success(){

        RetStatus retStatus = new RetStatus();
        retStatus.setRetCode(StatusCode.SUCCESS.getCode());
        retStatus.setErrMsg(AppContants.localeMessageService.getMessage("request.success"));

        return new ResponseData(retStatus).data(new Object());
    }
    /**
     * 设置成功状态
     *
     * @date  16/8/18 AM10:50
     * @return ResponseData<T>
     */
    public static <T> ResponseData<T> success(T data){

        RetStatus retStatus = new RetStatus();
        retStatus.setRetCode(StatusCode.SUCCESS.getCode());
        retStatus.setErrMsg(AppContants.localeMessageService.getMessage("request.success"));

        return new ResponseData<T>(retStatus,data);
    }

    public boolean checkSuccess(){
        if (getRetStatus() == null){
            return false;
        }
        return StatusCode.SUCCESS.getCode().equals(getRetStatus().getRetCode());
    }

    public ResponseData<T> fail(){
        if (getRetStatus() == null){
            setRetStatus(new RetStatus());
        }
        this.retStatus.setRetCode(StatusCode.FAILED.getCode());
        this.retStatus.setErrMsg(AppContants.localeMessageService.getMessage("request.failed"));
        this.data((T)new Object());

        return this;
    }

    public ResponseData<T> fail(String errorCode){
        if (getRetStatus() == null){
            setRetStatus(new RetStatus());
        }
        this.retStatus.setRetCode(errorCode);
        this.data((T)new Object());
        return this;
    }

    public ResponseData<T> fail(StatusCode statusCode){
        if (getRetStatus() == null){
            setRetStatus(new RetStatus());
        }
        this.retStatus.setRetCode(statusCode.getCode());
        this.retStatus.setErrMsg(AppContants.localeMessageService.getMessage(statusCode.name()));
        this.data((T)new Object());
        return this;
    }

    /**
     * 失败消息
     *
     * @param msg 失败消息
     * @date  16/8/18 AM10:50
     * @return ResponseData<T>
     */
    public ResponseData<T> msg(String msg){
        if (getRetStatus() == null){
            setRetStatus(new RetStatus());
        }
        this.retStatus.setErrMsg(msg);
        return this;
    }

    /**
     * 设置响应数据
     *
     * @param data 响应结果
     * @date  16/8/18 AM10:50
     * @return ResponseData<T>
     */
    public ResponseData<T> data(T data){
        this.retData = data;
        return this;
    }
    /**
     * 设置响应状态
     *
     * @param retStatus 响应状态
     * @date  09/11/18 AM10:50
     * @return ResponseData<T>
     */
    public ResponseData<T> status(RetStatus retStatus){
        this.retStatus = retStatus;
        this.data((T)new Object());
        return this;
    }

    /**
     * 处理失败
     *
     * @date  19/11/18 AM10:50
     * @return ResponseData<T>
     */
    public static ResponseData failure(){
        return new ResponseData().fail().data(new Object());
    }

    /**
     * 处理失败
     *
     * @date  19/11/18 AM10:50
     * @return ResponseData<T>
     */
    public static ResponseData failure(String errorCode){
        return new ResponseData().fail(errorCode).data(new Object());
    }

    /**
     * 处理失败
     *
     * @date  20191009 18:23:12
     * @return ResponseData<T>
     */
    public static ResponseData failure(StatusCode statusCode){
        return new ResponseData().fail(statusCode).data(new Object());
    }

}
